package cn.itcast.ft.controller;

import cn.hutool.extra.mail.MailUtil;
import cn.hutool.http.HttpRequest;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import cn.itcast.config.VisibleThreadPoolTaskExecutor;
import cn.itcast.ft.dto.*;
import cn.itcast.ft.entity.FTToken;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.math.BigDecimal;
import java.net.Proxy;
import java.util.ArrayList;
import java.util.List;

@RestController
@Slf4j
public class JZKJController {
    private static List<FTToken> tokenList=new ArrayList<>();
    @Autowired
    private VisibleThreadPoolTaskExecutor executor;
    @RequestMapping("/jz_kjCreateMarket")
    public String wzhCreateMarket(@RequestParam(value = "num",defaultValue = "10000") Integer num,
                               @RequestParam(value = "salePrice",defaultValue = "29") String salePrice,
                               @RequestParam(value = "goodsId",defaultValue = "24") String goodsId) {
        FTToken token4=new FTToken();
        String token="eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpYXQiOjE3MTczODgzMTksIm5iZiI6MTcxNzM4ODMxOSwiZXhwIjoxNzE3OTkzMTE5LCJhdWQiOiJtZW1iZXIiLCJpc3MiOiIiLCJqdGkiOiI4OTExNTU1NTBmYmYxYjc4MWEwNGJkYTBkYTc2MjBiMSIsInVpZCI6IkJQVCJ9.0536fe5f16335cf220a286ec116ce4c9246b83e77f731c8521a428dffc3fa4bf";
        token4.setToken(token);
        token4.setRefreshToken("微信名字:万");
        tokenList.add(token4);
        if(!CollectionUtils.isEmpty(tokenList)) {
            for (int i = 0; i < num; i++) {
                try {
                    for (int z = 0; z < tokenList.size(); z++) {
                        Proxy proxy = null;
                        confireMarketOrder(proxy, tokenList.get(z), goodsId, salePrice);
                        Thread.sleep(3000);
                    }
                } catch (Exception e) {
                    log.error("#####异常####" + e.getMessage());
                }
            }
            log.info("####FT执行完成#####");
        }
        return null;
    }
    public void confireMarketOrder(Proxy proxy,FTToken token,String goodsId,String salePrice){
        try{
            createMarketOrder(proxy,token,goodsId,salePrice);
        }catch (Exception e){
            log.error("#####获取商品失败####"+e.getMessage()+"###"+token);
        }
    }
    public void createMarketOrder(Proxy proxy,FTToken token,String goodsId,String salePrice) throws InterruptedException {
        String response=null;
        JZKJOrderDTO goodsDTO=null;
        try{
            JSONObject jsonObject=new JSONObject();
            jsonObject.putOnce("goods_id",goodsId);
            response = HttpRequest.post("https://api.xmchenjie.top/member/order/order/lowestMarketOrder")
                    .header("Client-Version","1.0.0")
                    .header("Client-Os","h5")
                    .header("Face-To","member")
                    .header("Token",token.getToken())
                    .body(jsonObject.toString())
                    .execute().body();
            log.info("#####确认订单结果####"+response);
            goodsDTO= JSONUtil.toBean(response,JZKJOrderDTO.class);
        }catch (Exception e){
            log.error("##获取商品数据异常###:"+e.getMessage()+"####返回数据：###"+response);
        }
        if(goodsDTO.getCode()==200){
            MailUtil.send("1027060531@qq.com", "收到订单", "您有一个订单:"+token.getRefreshToken()+"，请赶紧支付！", false);
        }
    }
    @RequestMapping("/jz_kj_addUser")
    public String wzhCreateMarket(
            @RequestParam(value = "account") String account,
            @RequestParam(value = "wx") String wx,
            @RequestParam(value = "password") String password) {
//        String response=null;
//        WKUserDTO goodsDTO=null;
//        try{
//            JSONObject jsonObject=new JSONObject();
//            jsonObject.putOnce("account",account);
//            jsonObject.putOnce("password",password);
//            response = HttpRequest.post("https://duoliang.cc/api/login")
//                    .body(jsonObject.toString())
//                    .execute().body();
//            log.info("#####确认订单结果####"+response);
//            goodsDTO= JSONUtil.toBean(response,WKUserDTO.class);
//        }catch (Exception e){
//            log.error("##获取商品数据异常###:"+e.getMessage()+"####返回数据：###"+response);
//        }
//        if(goodsDTO.getStatus()==200){
//            FTToken token4=new FTToken();
//            String token="Bearer "+goodsDTO.getData().getToken();
//            token4.setToken(token);
//            token4.setRefreshToken("微信名字:"+wx);
//            tokenList.add(token4);
//        }else{
//            log.error("##账号密码错###:"+wx);
//        }
        FTToken token4=new FTToken();
        String token="eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpYXQiOjE3MTczODgzMTksIm5iZiI6MTcxNzM4ODMxOSwiZXhwIjoxNzE3OTkzMTE5LCJhdWQiOiJtZW1iZXIiLCJpc3MiOiIiLCJqdGkiOiI4OTExNTU1NTBmYmYxYjc4MWEwNGJkYTBkYTc2MjBiMSIsInVpZCI6IkJQVCJ9.0536fe5f16335cf220a286ec116ce4c9246b83e77f731c8521a428dffc3fa4bf";
        token4.setToken(token);
        token4.setRefreshToken("微信名字:"+wx);
        tokenList.add(token4);
        return null;
    }
}
